<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>06-数组排序</title>
</head>
<body>
    <script>
        //  无参，默认按字符串排序
        let arr = [22,3,54,12,442,56];
        arr.sort();
        console.log(arr);  // [12, 22, 3, 442, 54, 56]

        //  数字排序
        let arr1 = [22,13,4,32];
        arr1.sort(function(a,b){
            return a - b;
        });
        console.log(arr1);  // [4, 13, 22, 32]

        // 等同于
        var numbers = [4, 2, 5, 1, 3];
        numbers.sort((a, b) => a - b);
        console.log(numbers);

        // 作业：对象属性排序
        // 商品的价格，距离，好评，综合

        var items = [
        { name: 'Edward', value: 21 },
        { name: 'Sharpe', value: 37 },
        { name: 'And', value: 45 },
        { name: 'The', value: 12 },
      ];
/* 
      // sort by value
      items.sort(function (a, b) {
        return (a.value - b.value)
      });
      console.log(items); */

// sort by name
items.sort(function(a, b) {
  var nameA = a.name.toUpperCase(); // ignore upper and lowercase
  var nameB = b.name.toUpperCase(); // ignore upper and lowercase
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  // names must be equal
  return 0;
});
console.log(items);
    </script>
</body>
</html>